package main

import (
	"2301a/yuekao/api"
	"2301a/yuekao/api/handler"
	"2301a/yuekao/config"
	"2301a/yuekao/consts"
	"2301a/yuekao/inits"
	"2301a/yuekao/internal/model"
	"2301a/yuekao/internal/queue"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
)

func main() {
	config.InitsConfig()
	inits.InitMysql()
	inits.InitRabbitMq()
	go TestPublishUpdate()
	router := gin.Default()
	api.LoadRouter(router)
	router.Run(":9999") // 监听并在 0.0.0.0:8080 上启动服务
}
func TestPublishUpdate() {
	inits.InitRabbitMq()

	msgs := queue.PublishSub(consts.HOUSE_PRICE_UPDATE)

	forever := make(chan bool)
	//启用协程处理消息
	go func() {
		for d := range msgs {
			//消息逻辑处理，可以自行设计逻辑
			log.Printf("Received a message: %s", d.Body)
			var data model.House
			_ = json.Unmarshal(d.Body, &data)

			for _, conn := range handler.OnlineUser {

				handler.WSRespSuccess(conn, fmt.Sprintf("%s价格调整%d万", data.Title, data.Price/10000))

			}
		}
	}()

	log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
	<-forever
}
